package com.controller;

import com.entity.Employee;
import com.service.EmployeeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class Add {
    @Resource
    private EmployeeService employeeService;

    @GetMapping("/add")
    public String add(Employee employee, Model model){
        int ann = employeeService.addEmployee(employee);
        if (ann>0){
            return "/list";
        }else {
            return "/index.jsp";
        }

    }

    @GetMapping("/list")
    public ModelAndView list(){
        List<Employee> employees = employeeService.finEmployee();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("list",employees);
        modelAndView.setViewName("/add.jsp");
        return modelAndView;
    }

    //@RequestParam()name=>传输值的name值
    @GetMapping("/del")
    public String del(@RequestParam(name = "id",required = true) int id, Model model){
        int dela = employeeService.del(id);
        if (dela>0){
            model.addAttribute("sss","删除成功！");
            return "/list";
        }else {
            model.addAttribute("sss","删除失败！");
            return "/list";
        }

    }
}
